import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { SecTimer } from "../../../com/gengine/core/frame/SecTimer";
import { SBattlefieldOpen } from "../../../Message/Public/SBattlefieldOpen";
import { SPeaceFieldList } from "../../../Message/Public/SPeaceFieldList";
import { SPeaceFieldHurtList } from "../../../Message/Public/SPeaceFieldHurtList";
import { SAttribute } from "../../../Message/Public/SAttribute";
import { Language } from "../../../extend/language/Language";
import { ClockManager } from "../manager/ClockManager";
import { PeaceFieldIconView } from "../view/peaceField/PeaceFieldIconView";
import { PeaceFieldEnterPanel } from "../view/peaceField/PeaceFieldEnterPanel";
import { PeaceFieldDamageRank } from "../view/peaceField/PeaceFieldDamageRank";
import { LeftTimeView } from "../view/common/LeftTimeView";
//class PeaceFieldController
    
    export  class PeaceFieldController extends Controller
    {
       

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.PeaceFieldShowEnterPanel, this.onPeaceFieldShowEnterPanel);
            Dispatcher.addEventListener(EventName.PeaceFieldEnter, this.onPeaceFieldEnter);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            NetDispatcher.addCmdListener(ServerCommand.PeaceFieldOpenCommand, this.onPeaceFieldOpenHandler);
            NetDispatcher.addCmdListener(ServerCommand.PeaceFieldCloseCommand, this.onPeaceFieldCloseHandler);
            NetDispatcher.addCmdListener(ServerCommand.PeaceFieldListCommand, this.onPeaceFieldListHandler);
            NetDispatcher.addCmdListener(ServerCommand.PeaceFieldHurtListCommand, this.onPeaceFieldHurtListHandler);
            NetDispatcher.addCmdListener(ServerCommand.PeaceFieldRefreshBossTimeCommand, this.onPeaceFieldRefreshBossTimeHandler);
            return;
        }

        /* internal  */onPeaceFieldShowEnterPanel(arg1: DataEvent): void
        {
            if (this.peaceFieldEnterPanel.isHide) 
            {
                this.peaceFieldEnterPanel.show();
                GameProxy.peaceField.getPeaceFieldList();
            }
            return;
        }

        /* internal  */onPeaceFieldEnter(arg1: DataEvent): void
        {
            GameProxy.peaceField.enterPeaceFieldList(arg1.data as SEntityId);
            return;
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            if (GameMapUtil.isPeaceField()) 
            {
                if (!this._isLastPeaceField) 
                {
                    this._isLastPeaceField = true;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TaskTrackShowOrHide, false));
                    this.peaceFieldDamageRank.show();
                    this.peaceFieldIconView.hide();
                    if (this._isBossRefresh) 
                    {
                        this.showLeftTime();
                    }
                    this.peaceFieldEnterPanel.hide();
                }
            }
            else if (this._isLastPeaceField) 
            {
                this._isLastPeaceField = false;
                if (this._inInActive) 
                {
                    this.peaceFieldIconView.show();
                }
                this.leftTimeView.hide();
                this.peaceFieldDamageRank.hide();
            }
            return;
        }

        /* internal  */onComplete(arg1: SecTimer): void
        {
            this._inInActive = false;
            this.peaceFieldIconView.hide();
            return;
        }

        /* internal  */onPeaceFieldOpenHandler(arg1: SBattlefieldOpen): void
        {
            this._inInActive = true;
            this._peaceFieldOpen = arg1;
            this.peaceFieldIconView.show();
            return;
        }

        /* internal  */onPeaceFieldCloseHandler(arg1: Object): void
        {
            this._inInActive = false;
            this.peaceFieldIconView.hide();
            return;
        }

        /* internal  */onPeaceFieldListHandler(arg1: SPeaceFieldList): void
        {
            this.peaceFieldEnterPanel.updateFields(cache.peaceField.peaceFieldEnterDataList);
            return;
        }

        /* internal  */onPeaceFieldHurtListHandler(arg1: SPeaceFieldHurtList): void
        {
            this.peaceFieldDamageRank.updateDamageList(cache.peaceField.aryDamageData);
            return;
        }

        /* internal  */onPeaceFieldRefreshBossTimeHandler(arg1: SAttribute): void
        {
            if (arg1.value > 0) 
            {
                this._isBossRefresh = false;
                this.leftTimeView.setParse(Language.getString(43300));
                this.leftTimeView.show();
                this.leftTimeView.updateLeftTime(arg1.value);
                this.leftTimeView.tfLeftTime.addEventListener(EventName.SecViewTimeChange, this.onTimeChange);
            }
            return;
        }

        /* internal  */onTimeChange(arg1: DataEvent): void
        {
            if (arg1.data == 0) 
            {
                this.leftTimeView.tfLeftTime.removeEventListener(EventName.SecViewTimeChange, this.onTimeChange);
                this._isBossRefresh = true;
                this.showLeftTime();
            }
            return;
        }

        /* internal  */showLeftTime(): void
        {
            if (this._peaceFieldOpen) 
            {
                this.leftTimeView.setParse(Language.getString(43301));
                this.leftTimeView.show();
                this.leftTimeView.updateLeftTime((this._peaceFieldOpen.endDt.time - ClockManager.instance.nowDate.time) / 1000);
            }
            return;
        }

        /* internal  */get peaceFieldIconView(): PeaceFieldIconView
        {
            if (!this._peaceFieldIconView) 
            {
                this._peaceFieldIconView = new PeaceFieldIconView();
            }
            return this._peaceFieldIconView;
        }

        /* internal  */get peaceFieldEnterPanel(): PeaceFieldEnterPanel
        {
            if (!this._peaceFieldEnterPanel) 
            {
                this._peaceFieldEnterPanel = new PeaceFieldEnterPanel();
            }
            return this._peaceFieldEnterPanel;
        }

        /* internal  */get peaceFieldDamageRank(): PeaceFieldDamageRank
        {
            if (!this._peaceFieldDamageRank) 
            {
                this._peaceFieldDamageRank = new PeaceFieldDamageRank();
            }
            return this._peaceFieldDamageRank;
        }

        /* internal  */get leftTimeView(): LeftTimeView
        {
            if (!this._leftTimeView) 
            {
                this._leftTimeView = new LeftTimeView();
            }
            return this._leftTimeView;
        }

        public resetPosition(): void
        {
            this.leftTimeView.resetPosition();
            this.peaceFieldDamageRank.resetPosition();
            return;
        }

        public isInitView(): Boolean
        {
            if (this._peaceFieldIconView) 
            {
                return true;
            }
            return false;
        }

        private /* var */_isLastPeaceField: Boolean=false;

        private /* var */_peaceFieldOpen: SBattlefieldOpen;

        private /* var */_activeTime: SecTimer;

        private /* var */_inInActive: Boolean=false;

        private /* var */_isBossRefresh: Boolean=true;

        private /* var */_peaceFieldIconView: PeaceFieldIconView;

        private /* var */_peaceFieldEnterPanel: PeaceFieldEnterPanel;

        private /* var */_peaceFieldDamageRank: PeaceFieldDamageRank;

        private /* var */_leftTimeView: LeftTimeView;
    }
